import Vue from 'vue'
import axios from 'axios'
import ViewUI from 'view-design'
import Antd from 'ant-design-vue'
import { BootstrapVue, IconsPlugin } from 'bootstrap-vue'
import ElementUI from 'element-ui'
import moment from 'moment'
import preview from 'vue-photo-preview'
import App from './App'
import store from './store'
import router from './router'
import 'element-ui/lib/theme-chalk/index.css'
import 'view-design/dist/styles/iview.css'

import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
import 'animate.css/animate.css'
import 'ant-design-vue/dist/antd.css'
import 'vue-photo-preview/dist/skin.css'

Vue.use(preview)

Vue.use(ElementUI)
Vue.use(Antd)

Vue.use(BootstrapVue)
Vue.use(IconsPlugin)

Vue.config.productionTip = false
Vue.use(ViewUI)

// 定义全局过滤器（时间格式化）
// 设置语言 (全局)
moment.locale('zh-cn')
Vue.filter('dataFormat', (dataStr) => moment(dataStr).utcOffset(0).format('YYYY-MM-DD HH:mm:ss'))

// Vue.filter('msgFormat', (msg) => msg.replace(/^(\d{6})\d{8}(\d{4})/, '$1******$2'))

// 设置基础URL
axios.defaults.baseURL = 'http://localhost:8080/'
// 设置请求超时时间
// axios.defaults.timeout = 5000

Vue.prototype.$axios = axios

// 对路由进行拦截
router.beforeEach((to, from, next) => {
  // 需要登录并且需要拥有 business 角色（business角色的用户才能访问）
  if (to.meta.requireAuth && to.meta.requireRole === 'business') {
    let users = JSON.parse(localStorage.getItem('user'))
    if (users === null) {
      // 未登录
      next({ path: '/login' })
    } else if (users.uRole !== 'business') {
      // 登录了但是没有 business 权限
      next({ path: '/' })
    } else {
      // 登陆了并且拥有business权限:对该路由放行
      next()
    }
    // 需要登录并且需要拥有 admin 角色（admin角色的用户才能访问）
  } else if (to.meta.requireAuth && to.meta.requireRole === 'admin') {
    let user = JSON.parse(localStorage.getItem('admin'))
    if (user === null) {
      // 判断是否登录
      next({ path: '/login' })
    } else if (user.uRole !== 'admin') {
      // 登陆了但是没有 admin 权限
      next({ path: '/' })
    } else {
      next()
    }
    // 需要登录权限
  } else if (to.meta.requireAuth) {
    let user = JSON.parse(localStorage.getItem('user'))
    if (user === null) {
      next({ path: '/login' })
    } else {
      next()
    }
  } else {
    next()
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
